/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dca.sistemascontrole.malhadecontrole;

import dca.sistemascontrole.domain.VariaveisDeControle;

/**
 *
 * @author Rafael
 */
public class ControladorPI_D extends MalhaFechada {

    private double kp, ki, kd;
    private double up, ui, ud;
    private double integralPID;
    private double nivelAnteriorPI_D;

    public ControladorPI_D(double setpoint, double kpe, double kie, double kde) {
        super(setpoint);
        this.kp = kpe;
        this.kd = kde;
        this.ki = kie;
        integralPID = 0;
        nivelAnteriorPI_D = Double.MIN_VALUE;
    }

    @Override
    protected VariaveisDeControle getNextValue(long t, double nivelTanqueSuperior, double nivelTanqueInfeior) {
        double erro;
        double U;
        erro = variaveisDeControle.getSetPoint() - nivelTanqueSuperior;
        integralPID = integralPID + ki * erro * 0.1;
        if (nivelAnteriorPI_D != Double.MIN_VALUE) {
            ud = kd * (nivelTanqueSuperior - nivelAnteriorPI_D);
        } else {
            ud = 0;
        }

        up = kp * erro;
        ui = integralPID;
        U = up + ui + ud;

        nivelAnteriorPI_D = nivelTanqueSuperior;

        variaveisDeControle.setU(U);
        variaveisDeControle.setUd(ud);
        variaveisDeControle.setUi(ui);
        variaveisDeControle.setUp(up);
        variaveisDeControle.setErro(erro);
        
        return variaveisDeControle;
    }
}
